package cc.catface.smsdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsMapper smsMapper;

    @Override
    public void saveCode(String phoneid, String code) {
        smsMapper.saveCode(phoneid, code, "" + System.currentTimeMillis());
    }

    @Override
    public String checkscode(String phoneid, String code) {
        //当前时间-2分钟小于发送短信时间
        SmsDto phoneInfo = smsMapper.getPhoneInfo(phoneid);
        if (phoneInfo == null || phoneInfo.getPhoneid() == null || phoneInfo.getPhoneid().length() < 5) {
            return "手机号错误！";
        }
        Long dbTimestamp = Long.valueOf(phoneInfo.getTimestamp());
        if ((System.currentTimeMillis() - 120000) > dbTimestamp) {
            return "验证码已过期！";
        }
        String checkCode = phoneInfo.getCode();
        if (!code.equals(checkCode)) {
            return "验证码错误！";
        }

        return "success";
    }
}
